package com.loie.xcx.web.controller;

import com.alibaba.fastjson2.JSONObject;
import com.loie.xcx.aop.SignatureValidation;
import com.loie.xcx.comm.JsonResult;
import com.loie.xcx.model.bo.AuthorityBO;
import com.loie.xcx.model.bo.IndexQueryNumBO;
import com.loie.xcx.model.bo.UserInfoAuthorityBO;
import com.loie.xcx.model.bo.UserInfoBO;
import com.loie.xcx.service.SystemService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhl
 */
@Slf4j
@RequestMapping("/sys")
@RestController
@AllArgsConstructor
public class SystemController {

    private final SystemService systemService;

    @SignatureValidation
    @PostMapping("/login")
    public JsonResult<UserInfoBO> login(@RequestBody String jsonStr) {

        String openId = JSONObject.parseObject(jsonStr, UserInfoBO.class).getOpenId();
        System.out.println(openId);
        UserInfoAuthorityBO bo = systemService.getAuthorityInfoByOpenId(openId);
        System.out.println(bo);

        if (bo != null && StringUtils.isNotBlank(bo.getUserId())) {
            UserInfoBO userInfoBO = new UserInfoBO();
            BeanUtils.copyProperties(bo, userInfoBO);
            AuthorityBO authorityBO = new AuthorityBO();
            BeanUtils.copyProperties(bo, authorityBO);
            userInfoBO.setAuthority(authorityBO);
            return new JsonResult<>(userInfoBO);
        }
        return new JsonResult<>(400, "登录失败");
    }

    @SignatureValidation
    @PostMapping("/bind")
    public JsonResult<String> bind(@RequestBody String jsonStr) {

        UserInfoBO bo = JSONObject.parseObject(jsonStr, UserInfoBO.class);
        if (StringUtils.isNotBlank(bo.getOpenId()) && StringUtils.isNotBlank(bo.getPhone()) && StringUtils.isNotBlank(bo.getUserId())) {
            if (systemService.setUserInfoWxByOpenId(bo) == 1) {
                return new JsonResult<>();
            }
        }
        return new JsonResult<>(400, "绑定失败");
    }

    @SignatureValidation
    @PostMapping("/unBind")
    public JsonResult<String> unBind(@RequestBody String jsonStr) {

        String openId = JSONObject.parseObject(jsonStr, UserInfoBO.class).getOpenId();
        if (StringUtils.isNotBlank(openId)) {
            if (systemService.delUserInfoWxByOpenId(openId) == 1) {
                return new JsonResult<>();
            }
            return new JsonResult<>();
        }
        return new JsonResult<>(400, "解绑失败");
    }

    @SignatureValidation
    @PostMapping("/userQuery")
    public JsonResult<UserInfoBO> userQuery(@RequestBody String jsonStr) {

        String phone = JSONObject.parseObject(jsonStr, UserInfoBO.class).getPhone();
        if (StringUtils.isNotBlank(phone)) {
            UserInfoBO bo = systemService.getUserInfoByPhone(phone);
            return new JsonResult<>(bo);
        }
        return new JsonResult<>(400, "查询失败");
    }

    @SignatureValidation
    @PostMapping("/indexQueryNum")
    public JsonResult<IndexQueryNumBO> indexQueryNum(@RequestBody String jsonStr) {

        String openId = JSONObject.parseObject(jsonStr, IndexQueryNumBO.class).getOpenId();
        if (StringUtils.isNotBlank(openId)) {
            IndexQueryNumBO bo = systemService.getIndexQueryNum(openId);
            return new JsonResult<>(bo);
        }
        return new JsonResult<>(400, "查询失败");
    }

}
